package whf.framework.report;

import java.io.File;
import java.util.Properties;

import whf.framework.util.StringUtils;

/**
 * @author king
 * @modify Dec 20, 2007 9:30:31 PM
 */
public class ReportConstants {
	public static String EXPORT_FILE_NAME = "en_";

	public static String EXPORT_TYPE = "et_";

	public static String EXPORT_HEADER_TITLE = "eht_";

	public final static String EXPORT = "e_";

	public final static String MIMETYPES_PROPERTIES = "mimeTypes.properties";

	private static Properties properties;
	
	/**
	 * 导出路径
	 * @property String:EXPORT_FILES
	 */
	public final static String EXPORT_FILES = "report.export.files";
	
	/**
	 * 导出pdf时的标题字体路径
	 * @property String:EXPORT_PDF_TITLE_FONT
	 */
	public final static String EXPORT_PDF_TITLE_FONT = "report.export.pdf.title-font";
	
	/**
	 * 导出pdf时的内容字体路径
	 * @property String:EXPORT_PDF_BODY_FONT
	 */
	public final static String EXPORT_PDF_BODY_FONT = "report.export.pdf.body-font";
	
	private static void init() {
		if (properties != null) {
			return;
		}
		try {
			properties = new Properties();
			properties.load(new ReportConstants().getClass().getResourceAsStream(MIMETYPES_PROPERTIES));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getFileMimeType(File file) {
		if (file == null) {
			return null;
		}
		return getFileMimeType(file.getName());
	}

	public static String getFileMimeType(String fileName) {
		if (StringUtils.isEmpty(fileName) || (fileName.indexOf(".") == -1)) {
			return null;
		}
		fileName = fileName.substring(fileName.lastIndexOf("."));
		return getExtensionMimeType(fileName);
	}

	public static String getExtensionMimeType(String extension) {
		String result = null;
		if (StringUtils.isEmpty(extension)) {
			return result;
		}
		init();
		extension = extension.toLowerCase();
		if (!extension.startsWith(".")) {
			extension = "." + extension;
		}
		result = (String) properties.get(extension);
		return result;
	}

	public static String escapeChars(String value) {
		if (StringUtils.isEmpty(value))
			return "";
		if (value.indexOf("&")>=0) {
			value = StringUtils.replace(value, "&", "&#38;");
		}
		if (value.indexOf(">")>=0) {
			value = StringUtils.replace(value, ">", "&gt;");
		}
		if (value.indexOf("<")>=0) {
			value = StringUtils.replace(value, "<", "&lt;");
		}
		return value;
	}
}
